home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 97 / CD-ROM 97 / CD-ROM 97.iso / internet / ghostzilla / ghsetup.exe / chrome / comm.jar / content / editor / EdAEJSEAttributes.js < prev    next >
Encoding:
JavaScript  |  2002-04-09  |  6.5 KB  |  231 lines

  1. /*
  2.  * The contents of this file are subject to the Netscape Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/NPL/
  6.  *
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  *
  12.  * The Original Code is Mozilla Communicator client code, released
  13.  * March 31, 1998.
  14.  *
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation. Portions created by Netscape are
  17.  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *   Ben "Count XULula" Goodger
  22.  */
  23.  
  24. function BuildJSEAttributeNameList()
  25. {
  26.   ClearMenulist(gDialog.AddJSEAttributeNameList);
  27.   
  28.   // Get events specific to current element
  29.   var elementName = gElement.localName.toLowerCase();
  30.   if (elementName in gJSAttr)
  31.   {
  32.     var attNames = gJSAttr[elementName];
  33.     var i;
  34.     var popup;
  35.     var sep;
  36.  
  37.     if (attNames && attNames.length)
  38.     {
  39.       // Since we don't allow user-editable JS events yet (but we will soon)
  40.       //  simply remove the JS tab to not allow adding JS events
  41.       if (attNames[0] == "noJSEvents")
  42.       {
  43.         var tab = document.getElementById("tabJSE");
  44.         if (tab)
  45.           tab.parentNode.removeChild(tab);
  46.  
  47.         return;
  48.       }
  49.  
  50.       for (i = 0; i < attNames.length; i++)
  51.         AppendStringToMenulist(gDialog.AddJSEAttributeNameList, attNames[i]);
  52.  
  53.       popup = gDialog.AddJSEAttributeNameList.firstChild;
  54.       if (popup)
  55.       {
  56.         sep = document.createElementNS(XUL_NS, "menuseparator");
  57.         if (sep)
  58.           popup.appendChild(sep);
  59.       }        
  60.     }
  61.   }
  62.  
  63.   // Always add core JS events unless we aborted above
  64.   for (i = 0; i < gCoreJSEvents.length; i++)
  65.   {
  66.     if (gCoreJSEvents[i] == "-")
  67.     {
  68.       if (!popup)
  69.         popup = gDialog.AddJSEAttributeNameList.firstChild;
  70.  
  71.       sep = document.createElementNS(XUL_NS, "menuseparator");
  72.  
  73.       if (popup && sep)
  74.         popup.appendChild(sep);
  75.     }
  76.     else
  77.       AppendStringToMenulist(gDialog.AddJSEAttributeNameList, gCoreJSEvents[i]);
  78.   }
  79.   
  80.   gDialog.AddJSEAttributeNameList.selectedIndex = 0;
  81.  
  82.   // Use current name and value of first tree item if it exists
  83.   onSelectJSETreeItem();
  84. }
  85.  
  86. // build attribute list in tree form from element attributes
  87. function BuildJSEAttributeTable()
  88. {
  89.   var nodeMap = gElement.attributes;
  90.   if (nodeMap.length > 0)
  91.   {
  92.     var added = false;
  93.     for (var i = 0; i < nodeMap.length; i++)
  94.     {
  95.       if( CheckAttributeNameSimilarity( nodeMap[i].nodeName, JSEAttrs ) )
  96.         continue;   // repeated or non-JS handler, ignore this one and go to next
  97.       if( !IsEventHandler( nodeMap[i].nodeName ) )
  98.         continue; // attribute isn't an event handler.
  99.       var name  = nodeMap[i].nodeName.toLowerCase();
  100.       var value = gElement.getAttribute(nodeMap[i].nodeName);
  101.       if (AddTreeItem( name, value, "JSEAList", JSEAttrs )) // add item to tree
  102.         added = true;
  103.     }
  104.  
  105.     // Select first item
  106.     if (added)
  107.       gDialog.AddJSEAttributeTree.selectedIndex = 0;
  108.   }
  109. }
  110.  
  111. // check to see if given string is an event handler.
  112. function IsEventHandler( which )
  113. {
  114.   var handlerName = which.toLowerCase();
  115.   var firstTwo = handlerName.substring(0,2);
  116.   if (firstTwo == "on")
  117.     return true;
  118.   else
  119.     return false;
  120. }
  121.  
  122. function onSelectJSEAttribute()
  123. {
  124.   if(!gDoOnSelectTree)
  125.     return;
  126.  
  127.   gDialog.AddJSEAttributeValueInput.value = 
  128.       GetAndSelectExistingAttributeValue(gDialog.AddJSEAttributeNameList.label, "JSEAList");
  129. }
  130.  
  131. function onSelectJSETreeItem()
  132. {
  133.   var tree = gDialog.AddJSEAttributeTree;
  134.   if (tree && tree.treeBoxObject.selection.count)
  135.   {
  136.     var name = GetTreeItemAttributeStr(getSelectedItem(tree));
  137.  
  138.     // Select attribute name in list
  139.     if (gDialog.AddJSEAttributeNameList.firstChild)
  140.     {
  141.       var arr = gDialog.AddJSEAttributeNameList.firstChild.getElementsByAttribute('label', name);
  142.       if (arr && arr.length)
  143.         gDialog.AddJSEAttributeNameList.selectedItem = arr[0];
  144.  
  145.       // Set value input to that in tree (no need to update this in the tree)
  146.       gUpdateTreeValue = false;
  147.       gDialog.AddJSEAttributeValueInput.value =  GetTreeItemValueStr(getSelectedItem(tree));
  148.       gUpdateTreeValue = true;
  149.     }
  150.   }
  151. }
  152.  
  153. function onInputJSEAttributeValue()
  154. {
  155.   if (gUpdateTreeValue)
  156.   {
  157.  
  158.     var name = TrimString(gDialog.AddJSEAttributeNameList.label);
  159.     var value = TrimString(gDialog.AddJSEAttributeValueInput.value);
  160.  
  161.     // Update value in the tree list
  162.     // Since we have a non-editable menulist, 
  163.     //   we MUST automatically add the event attribute if it doesn't exist
  164.     if (!UpdateExistingAttribute( name, value, "JSEAList" ) && value)
  165.       AddTreeItem( name, value, "JSEAList", JSEAttrs );
  166.   }
  167. }
  168.  
  169. function editJSEAttributeValue(targetCell)
  170. {
  171.   if (IsNotTreeHeader(targetCell))
  172.     gDialog.AddJSEAttributeValueInput.inputField.select();
  173. }
  174.  
  175. function UpdateJSEAttributes()
  176. {
  177.   var JSEAList = document.getElementById("JSEAList");
  178.   var i;
  179.  
  180.   // remove removed attributes
  181.   for (i = 0; i < JSERAttrs.length; i++)
  182.   {
  183.     name = JSERAttrs[i];
  184.     if (gElement.getAttribute(name))
  185.       doRemoveAttribute(gElement, name);
  186.   }
  187.  
  188.   // Add events
  189.   for (i = 0; i < JSEAList.childNodes.length; i++)
  190.   {
  191.     var item = JSEAList.childNodes[i];
  192.  
  193.     // set the event handler
  194.     doSetAttribute( GetTreeItemAttributeStr(item), GetTreeItemValueStr(item) );
  195.   }
  196. }
  197.  
  198. function RemoveJSEAttribute()
  199. {
  200.   var treechildren = gDialog.AddJSEAttributeTree.lastChild;
  201.  
  202.   // This differs from HTML and CSS panels: 
  203.   // We reselect after removing, because there is not
  204.   //  editable attribute name input, so we can't clear that
  205.   //  like we do in other panels
  206.   var newIndex = gDialog.AddJSEAttributeTree.selectedIndex;
  207.  
  208.   // We only allow 1 selected item
  209.   if (gDialog.AddJSEAttributeTree.treeBoxObject.selection.count)
  210.   {
  211.     var item = getSelectedItem(gDialog.AddJSEAttributeTree);
  212.  
  213.     // Name is the text of the treecell
  214.     var attr = GetTreeItemAttributeStr(item);
  215.  
  216.     // remove the item from the attribute array
  217.     if (newIndex >= (JSEAttrs.length-1))
  218.       newIndex--;
  219.  
  220.     // remove the item from the attribute array
  221.     JSERAttrs[JSERAttrs.length] = attr;
  222.     RemoveNameFromAttArray(attr, JSEAttrs);
  223.  
  224.     // Remove the item from the tree
  225.     treechildren.removeChild (item);
  226.  
  227.     // Reselect an item
  228.     gDialog.AddJSEAttributeTree.selectedIndex = newIndex;
  229.   }
  230. }
  231.